import go from 'gojs'
import { makeFont, getFontHeight } from '@/utils'

function partIsLink(part) {
  return part.type === go.Panel.Link
}

function partIsNode(part) {
  return part.type === go.Panel.Node || part.type === go.Panel.Spot
}

// 三点计算圆弧的弧度
function caclCurviness(spt, ept, pt) {
  const start = new go.Point(spt.x, spt.y)
  const end = new go.Point(ept.x, ept.y)
  const newpt = new go.Point(pt.x, pt.y)

  const ang = start.directionPoint(end)
  const mid = new go.Point((start.x + end.x) / 2, (start.y + end.y) / 2)
  const a = new go.Point(9999, 0).rotate(ang + 90).add(mid)
  const b = new go.Point(9999, 0).rotate(ang - 90).add(mid)
  const q = newpt.copy().projectOntoLineSegmentPoint(a, b)
  let curviness = Math.sqrt(mid.distanceSquaredPoint(q))
  const diff = mid.directionPoint(q) - ang
  if ((diff > 0 && diff < 180) || diff < -180) {
    curviness = -curviness
  }
  return curviness
}

// function getFirstSelectedPart(parts) {
//   if (parts == null || parts.count == 0) return null
//   if (parts.count > 1) return null
//   return parts.first()
// }

// function getFirstSelectedData(parts, type) {
//   var part = getFirstSelectedPart(parts)
//   if (part == null) return null

//   if (type == 'Link' && partIsLink(part)) {
//     return part.data
//   } else if (type == 'Node' && partIsNode(part)) {
//     return part.data
//   } else {
//     return null
//   }
// }

function filterParts(parts, type, reverse = false) {
  if (parts === null || parts.count === 0) {
    return null
  }
  const indexs = []
  let i = 0
  parts.forEach(part => {
    if (type === 'Link' && partIsLink(part)) {
      indexs.push(i)
    } else if (type === 'Node' && partIsNode(part)) {
      indexs.push(i)
    } else if (type === 'All') {
      indexs.push(i)
    } else {
      // part.isSelected = false
    }
    i += 1
  })

  const _parts = []
  if (!reverse) {
    indexs.forEach(i => {
      _parts.push(parts[i])
    })
  } else {
    const s = new Set(indexs)
    for (let i = 0; i < parts.length; i++) {
      if (!s.has(i)) {
        _parts.push(parts[i])
      }
    }
  }
  return _parts
}

function toFont(fontSize) {
  return makeFont(fontSize, false)
}

function fromFont(font) {
  return getFontHeight(font)
}

function toPoints(data, link) {
  const points = JSON.parse(data)
  const ptList = new go.List() // make a list of Points
  points.forEach(function(val) {
    ptList.add(new go.Point(val.x, val.y))
  })
  return ptList
}

function fromPoints(ptList, data, model) {
  const ptArray = []
  ptList.each(function(val) {
    ptArray.push({
      x: val.x,
      y: val.y
    })
  })
  // model.setDataProperty(data, key, JSON.stringify(ptArray));
  return JSON.stringify(ptArray)
}

// function fromPoints1(ptList, data, model) {
//     return fromPoints("line_points", ptList, data, model);
// }

// function fromPoints2(ptList, data, model) {
//     return fromPoints("spline_points", ptList, data, model);
// }

function toOrthogonal(data, link) {
  // console.log('set routing: '+data);
  return data === true ? go.Link.Orthogonal : go.Link.Normal
}

export default {
  partIsLink,
  partIsNode,
  caclCurviness,
  // getFirstSelectedPart,
  // getFirstSelectedData,
  filterParts,
  toPoints,
  fromPoints,
  toOrthogonal,
  toFont,
  fromFont
}
